package com.csii.pos.posplan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.util.excel.ExcelUtils;
import com.csii.pos.dto.demo.DemoRes;
import com.csii.pos.dto.posplan.PosPlanReq;
import com.csii.pos.dto.posplan.PosPlanRes;
import com.csii.pos.posplan.service.PosPlanService;
import com.csii.pos.query.AfSofUof;
import com.csii.pos.query.ExcelTemplate;
import com.csii.pos.query.service.IExcelTemplateService;
import com.csii.pos.query.vo.AfSofUofVO;
import com.csii.pos.query.vo.LqdtMntVO;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 用款预警信息查询controller
 * @author: liuzhonghua
 * @create:
 */
@RestController
@Slf4j
public class PosPlanController implements com.csii.pos.service.posplan.PosPlanService {
    @Resource
    private PosPlanService posPlanService;
    @Autowired
    private IExcelTemplateService excelTemplateService;
    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * @description:  用款预警信息查询的查询
     * @param: posPlan
     * @return:  Ipage<PosPlan></>
     * @author liuzhonghua
     * @date:
     */
    @Override
    public PosPlanRes queryPosPlan(PosPlanReq planReq) {
        Page<PosPlan> page = new Page<>(planReq.getPageNo(),planReq.getPageSize());
        PosPlan posPlan = new PosPlan();
        //参数类中转化为实体类
        posPlan.setPosPlanDt(planReq.getPosPlanDt());
        posPlan.setPosPlanItem(planReq.getPosPlanItem());
        //分页查询
        IPage<PosPlan> iPage = posPlanService.queryPosPlan(page,posPlan);
        PageVo pageVoFromIPage = PageVo.getPageVoFromIPage(iPage);
        PosPlanRes demoRes = new PosPlanRes();
        //数据存入返回值类中
        demoRes.setCode("200");
        demoRes.setData(pageVoFromIPage.getRecords());
        demoRes.setCurrentIndex(pageVoFromIPage.getCurrentIndex());
        demoRes.setRecordNumber(pageVoFromIPage.getRecordNumber());
        demoRes.setPageNo(pageVoFromIPage.getPageNo());
        demoRes.setPageSize(pageVoFromIPage.getPageSize());
        return demoRes;
    }
    /**
     * @description:  输入框查询是提示接口
     * @param: hintVo
     * @return:  模糊查询list
     * @author liuzhonghua
     * @date:
     */
    @RequestMapping("queryHint")
    public APIResult<?> queryHint(@RequestBody PosPlan posPlan) {
        QueryWrapper<PosPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT POS_PLAN_ITEM");
        if (!StringUtils.isEmpty(posPlan.getPosPlanItem())) {
            queryWrapper.like("POS_PLAN_ITEM", posPlan.getPosPlanItem());
        }
        if (!StringUtils.isEmpty(posPlan.getPosPlanDt())) {
            queryWrapper.eq("POS_PLAN_DT",posPlan.getPosPlanDt());
        }

        List<PosPlan> list = posPlanService.list(queryWrapper);
        return APIResult.successResult(list);
    }
    /**
     * 导出excel
     *
     * @param lqdtMntVO 参数
     */
    @RequestMapping(value = "/exportXls")
    public void exportXls(HttpServletResponse response, LqdtMntVO lqdtMntVO) {
        QueryWrapper<ExcelTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TABLE_NAME","POS_PLAN");
        ExcelTemplate template = excelTemplateService.getOne(queryWrapper);
        ExcelUtils.exportTemplate(template.getTemplateName(), template.getTemplateDir(), jdbcTemplate, response);
    }
}
